﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Library
{
	public class InputState
	{
		#region Fields
		public enum InputType { KeyboardAndMouse, MouseOnly, KeyboardOnly }
		private InputType _type;
		public InputType Type { get { return _type; } }

		private KeyboardState _currentKeyboardState;
		private KeyboardState _previousKeyboardState;

		private MouseState _currentMouseState;
		private MouseState _previousMouseState;

		public Vector2 MousePosition { get { return new Vector2(_currentMouseState.X, _currentMouseState.Y); } }
		#endregion

		#region Constructor
		public InputState(InputType type)
		{
			_type = type;
		}
		#endregion

		#region Methods
		public void UpdateStates()
		{
			_currentKeyboardState = Keyboard.GetState();
			_currentMouseState = Mouse.GetState();
		}
		public void UpdatePrevStates()
		{
			_previousMouseState = _currentMouseState;
			_previousKeyboardState = _currentKeyboardState;
		}
		#region Keyboard methods
		public bool IsKeyPressed(Keys key)
		{
			return _currentKeyboardState.IsKeyDown(key);
		}
		public bool IsKeyUp(Keys key)
		{
			return _currentKeyboardState.IsKeyUp(key);
		}
		public bool IsKeyNewPressed(Keys key)
		{
			return _currentKeyboardState.IsKeyDown(key) && _previousKeyboardState.IsKeyUp(key);
		}
		#endregion

		#region Mouse methods
		#region Left button
		public bool IsMouseLeftButtonPressed()
		{
			if (_currentMouseState.LeftButton == ButtonState.Pressed)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseLeftButtonReleased()
		{
			if (_currentMouseState.LeftButton == ButtonState.Released)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseLeftButtonNewPressed()
		{
			if (_currentMouseState.LeftButton == ButtonState.Pressed &&
				_previousMouseState.LeftButton == ButtonState.Released)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseLeftButtonNewReleased()
		{
			if (_currentMouseState.LeftButton == ButtonState.Released &&
				_previousMouseState.LeftButton == ButtonState.Pressed)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseLeftNewClickingSprite(Sprite sprite)
		{
			if (IsMouseLeftButtonNewPressed() && IsMouseOver(sprite))
			{
				return true;
			}
			return false;
		}
		#endregion
		#region Right button
		public bool IsMouseRightButtonPressed()
		{
			if (_currentMouseState.RightButton == ButtonState.Pressed)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseRightButtonReleased()
		{
			if (_currentMouseState.RightButton == ButtonState.Released)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseRightButtonNewPressed()
		{
			if (_currentMouseState.RightButton == ButtonState.Pressed &&
				_previousMouseState.RightButton == ButtonState.Released)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseRightButtonNewReleased()
		{
			if (_currentMouseState.RightButton == ButtonState.Released &&
				_previousMouseState.RightButton == ButtonState.Pressed)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseRightNewClickingSprite(Sprite sprite)
		{
			if (IsMouseRightButtonNewPressed() && IsMouseOver(sprite))
			{
				return true;
			}
			return false;
		}
		#endregion
		#region Middle Button
		public bool IsMouseMiddleButtonPressed()
		{
			if (_currentMouseState.MiddleButton == ButtonState.Pressed)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseMiddleButtonReleased()
		{
			if (_currentMouseState.MiddleButton == ButtonState.Released)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseMiddleButtonNewPressed()
		{
			if (_currentMouseState.MiddleButton == ButtonState.Pressed &&
				_previousMouseState.MiddleButton == ButtonState.Released)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseMiddleButtonNewReleased()
		{
			if (_currentMouseState.MiddleButton == ButtonState.Released &&
				_previousMouseState.MiddleButton == ButtonState.Pressed)
			{
				return true;
			}
			return false;
		}
		public bool IsMouseMiddleNewClickingSprite(Sprite sprite)
		{
			if (IsMouseMiddleButtonNewPressed() && IsMouseOver(sprite))
			{
				return true;
			}
			return false;
		}
		#endregion

		#region Misc Methods
		public bool IsMouseOver(Sprite sprite)
		{
			if ((_currentMouseState.X >= sprite.position.X) && _currentMouseState.X <= (sprite.position.X + sprite.Width) &&
					_currentMouseState.Y >= sprite.position.Y && _currentMouseState.Y <= (sprite.position.Y + sprite.Height))
				return true;

			return false;
		}
		#endregion
		#endregion

		//Saves the inputstate from reading input if it is not needed.
		public void ChangeInputType(InputType type)
		{
			_type = type;
		}
		#endregion
	}
}
